﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace MVCCentral.Framework
{
    public class ImageManager
    {
        private Size CalculateDimensions(Size oldSize, int targetSize)
        {
            Size newSize = new Size();

            if (oldSize.Height > oldSize.Width)
            {
                newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height));
                newSize.Height = targetSize;
            }
            else
            {
                newSize.Width = targetSize;
                newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Height));
            }

            return newSize;
        }

        private byte[] ResizeImageFile(Stream imageFile, int targetSize)
        {
            using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(imageFile))
            {
                Size newSize = CalculateDimensions(oldImage.Size, targetSize);
                using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
                {
                    using (Graphics canvas = Graphics.FromImage(newImage))
                    {
                        canvas.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                        canvas.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        canvas.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                        canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
                        MemoryStream m = new MemoryStream();
                        newImage.Save(m, ImageFormat.Jpeg);
                        return m.GetBuffer();
                    }
                }
            }
        }

        private Image ResizeImageFile(Image image, int targetSize)
        {
            using (System.Drawing.Image oldImage = image)
            {
                Size newSize = CalculateDimensions(oldImage.Size, targetSize);
                using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
                {
                    using (Graphics canvas = Graphics.FromImage(newImage))
                    {
                        canvas.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                        canvas.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        canvas.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                        canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
                    }
                    return newImage;
                }
            }
        }



        System.Drawing.Image FixedSize(System.Drawing.Image imgPhoto, int Width, int Height)
        {
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = (float)Width / (float)sourceWidth;
            nPercentH = (float)Height / (float)sourceHeight;
            if (nPercentH < nPercentW)
            {
                nPercent = nPercentH;
                destX = System.Convert.ToInt16(((Width - sourceWidth * nPercent) / 2));
            }
            else
            {
                nPercent = nPercentW;
                destY = System.Convert.ToInt16(((Height - sourceHeight * nPercent) / 2));
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.Clear(Color.White);
            grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }




        public void CreateThumbnail(string directory, string fileName, string contentType)
        {
            // There is a GetThumbnailImage method that is simpler; however image quality is lower
            System.Drawing.Image imgOriginal = System.Drawing.Image.FromFile((directory + fileName));
            System.Drawing.Image imgThumb = FixedSize(imgOriginal, 150, 150);
            imgThumb.Save(directory + ThumbNameFromFileName(fileName), ImageFormatFromContentType(contentType));
            imgThumb.Dispose();
            imgOriginal.Dispose();
        }


        private ImageFormat ImageFormatFromContentType(string ContentType)
        {
            ImageFormat format;
            switch (ContentType)
            {
                case "image/bmp":
                    format = ImageFormat.Bmp;
                    break;
                case "image/gif":
                    format = ImageFormat.Gif;
                    break;
                case "image/pjpeg":
                    format = ImageFormat.Jpeg;
                    break;
                case "image/jpeg":
                    format = ImageFormat.Jpeg;
                    break;
                case "image/png":
                    format = ImageFormat.Png;
                    break;
                default:
                    format = ImageFormat.Jpeg;
                    break;
            }
            return format;
        }


        public string ThumbNameFromFileName(string fileName)
        {
            int i = fileName.LastIndexOf(".");
            string baseFileName = fileName.Substring(0, i);
            int iStartPos = i + 1;
            string fileExtension = fileName.Substring(iStartPos, fileName.Length - iStartPos);
            string newFileName = baseFileName + "_thumb." + fileExtension;
            return newFileName;
        }

    }
}
